import tkinter
import tkinter.messagebox
import tkinter.ttk


#创建tkinter应用程序
root = tkinter.Tk()
#设置窗口标题
root.title('小傻瓜')
#定义窗口大小
root['height'] = 450
root['width'] = 370


XUKE = tkinter.StringVar()
XUKE.set('许克写的')

#与姓名关联的变量
varName = tkinter.StringVar()
varName.set('')
#创建标签然后放到窗口上
labelName = tkinter.Label(root,
                          text='姓名',
                          justify=tkinter.RIGHT,
                          width=50)
labelName.place(x=30,y=5,width=50,height=20)
#创建文本框，同时设置关联变量
entryName = tkinter.Entry(root,
                          width=120,
                          textvariable=varName)
entryName.place(x=70,y=5,width=120,height=20)

labelGrade = tkinter.Label(root,
                           text='年级',
                           justify = tkinter.RIGHT,width=50)
labelGrade.place(x=5,y=40,width=100,height=20)
#模拟学生所在年级，字典键为年级，值为班级
studentClasses = {'1年级':['1班','2班','3班','4班'],
                  '2年级':['1班','2班'],
                  '3年级':['1班','2班','3班']}
#学生年级组合框
comboGrade = tkinter.ttk.Combobox(root,width=50,
                                  values=tuple(studentClasses.keys()))
comboGrade.place(x=70,y=40,width=75,height=20)
#事件处理函数
def comboChange(event):
    grade = comboGrade.get()
    if grade:
        #动态改变组合框可选项
        comboClass["values"] = studentClasses.get(grade)
    else :
        comboClass.set([])
#绑定组合框事件处理函数
comboGrade.bind('<<ComboboxSelected>>' , comboChange)


labelClass = tkinter.Label(root,
                           text='班级',
                           justify=tkinter.RIGHT,width=50)
labelClass.place(x=170,y=40,width=50,height=20)
#学生班级组合框
comboClass = tkinter.ttk.Combobox(root,width=50)
comboClass.place(x=220,y=40,width=50,height=20)


labelSex = tkinter.Label(root,
                         text='性别',
                         justify=tkinter.RIGHT,width=50)
labelSex.place(x=10,y=70,width=100,height=20)
#与性别关联变量，1为雄，2为雌，默认雄
sex = tkinter.IntVar()
sex.set(1)
#单选钮，雄
redionMan = tkinter.Radiobutton(root,
                                variable=sex,
                                value=1,
                                text="男")
redionMan.place(x=70,y=70,width=50,height=20)
#单选钮，雌
redionWoman = tkinter.Radiobutton(root,
                                  variable=sex,
                                  value=0,
                                  text="女")
redionWoman.place(x=130,y=70,width=70,height=20)

#与是否班长关联变量，默认当前角色非班长
monitor = tkinter.IntVar()
monitor.set(0)
#复选框，选中时变量值为1，未选中时值为0
checkMonitor = tkinter.Checkbutton(root,
                                   text='是班长？',
                                   variable = monitor,
                                   onvalue=1,
                                   offvalue=0)
checkMonitor.place(x=20,y=100,width=100,height=20)
#添加按钮单击事件处理函数
def addInformation() :
    result = '名字' + entryName.get()
    result = result + '年级' + comboGrade.get()
    result = result + '班级' + comboClass.get()
    result = result + '性别' \
             + ('男' if sex.get() else '女')
    result = result + '班长' \
             + ('是' if monitor.get() else '否')
    #把信息插入到列表框组件中
    listboxStudents.insert(0,result)
buttonAdd = tkinter.Button(root,
                           text='添加内容',
                           width=40,
                           command = addInformation)
buttonAdd.place(x=130,y=100,width=100,height=20)

#删除按钮的处理事件
def deleteSelection() :
    selection = listboxStudents.curselection()
    if not selection :
        tkinter.messagebox.showinfo(title='信息',
                                    message='背水一战，没有选择')
    else :
        listboxStudents.delete(selection)
buttonDelete = tkinter.Button(root,
                              text='删除内容',
                              width=100,
                              command=deleteSelection)    
buttonDelete.place(x=230,y=100,width=100,height=20)


#创建列表框组件
listboxStudents = tkinter.Listbox(root,width=300)
listboxStudents.place(x=10,y=130,width=300,height=200)


#启动消息循环
root.mainloop()
